package com.ikun.mallbackend.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ikun.mallbackend.constant.SystemConstant;
import com.ikun.mallbackend.entity.Result;
import com.ikun.mallbackend.entity.WxUserInfo;
import com.ikun.mallbackend.properties.WeixinProperties;
import com.ikun.mallbackend.service.IWxUserInfoService;
import com.ikun.mallbackend.util.HttpClientUtil;
import com.ikun.mallbackend.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 微信用户Controller
 * @author java1234_小锋
 * @site www.java1234.com
 * @company 南通小锋网络科技有限公司
 * @create 2022-04-02 9:12
 */
@RestController
@RequestMapping("/user")
public class UserController {

    private final WeixinProperties weixinProperties;

    private final HttpClientUtil httpClientUtil;
    private final IWxUserInfoService wxUserInfoService;

    public UserController(WeixinProperties weixinProperties, HttpClientUtil httpClientUtil, IWxUserInfoService wxUserInfoService) {
        this.weixinProperties = weixinProperties;
        this.httpClientUtil = httpClientUtil;
        this.wxUserInfoService = wxUserInfoService;
    }

    /**
     * 微信用户登录
     * @return
     */
    @RequestMapping("/wxlogin")
    public Result wxLogin(@RequestBody WxUserInfo wxUserInfo){
        System.out.println(wxUserInfo.getCode());
        String jscode2sessionUrl=weixinProperties.getJscode2sessionUrl()+"?appid="+weixinProperties.getAppid()+"&secret="+weixinProperties.getSecret()+"&js_code="+wxUserInfo.getCode()+"&grant_type=authorization_code";
        String result = httpClientUtil.sendHttpGet(jscode2sessionUrl);
        JSONObject jsonObject = JSON.parseObject(result);
        String openid = jsonObject.get("openid").toString();
        System.out.println(openid);
        //1.用户插入或者更新到数据库
        WxUserInfo wxUser = wxUserInfoService.getOne(new QueryWrapper<WxUserInfo>().eq("openid", openid));
        if(wxUser!=null){
            //更新
            wxUser.setNickName(wxUserInfo.getNickName());
            wxUser.setAvatarUrl(wxUserInfo.getAvatarUrl());
            wxUser.setLastLoginDate(new Date());
            wxUserInfoService.updateById(wxUser);
        }else {
            //创建
            wxUserInfo.setOpenid(openid);
            wxUserInfo.setRegisterDate(new Date());
            wxUserInfo.setLastLoginDate(new Date());
            wxUserInfoService.save(wxUserInfo);
        }
        //2.利用jwt生成token返回给前端
        String token = JwtUtils.createJWT(openid, wxUserInfo.getNickName(), SystemConstant.JWT_TTL);
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("token",token);
        return Result.ok(resultMap);
    }
}
